package com.rf.HelloSpringBoot.service;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rf.HelloSpringBoot.domain.Visitor;
import com.rf.HelloSpringBoot.enums.ResultEnum;
import com.rf.HelloSpringBoot.exception.MyException;
import com.rf.HelloSpringBoot.repository.VisitorRepository;

/**
 * 
 * @author linzhengchun
 *
 * 2017年7月23日 下午10:55:16
 */
@Service
public class VisitorService {
	@Autowired
	private VisitorRepository visitorRepo;
	
	@Transactional
	public void insertTwo() {
		Visitor visitor1 = new Visitor();		
		visitor1.setName("Eric");
		visitor1.setAge(35);
		this.visitorRepo.save(visitor1);
		
		Visitor visitor2 = new Visitor();
		visitor2.setName("Lucyfsdfsfsdfsdf");
		visitor2.setAge(65);
		this.visitorRepo.save(visitor2);
	}
	
	public void getAge(Integer id) throws Exception{
		Visitor visitor = this.visitorRepo.findOne(id);
		Integer age = visitor.getAge();
		if (age < 10) {
			throw new MyException(ResultEnum.PRIMARY_SCHOOL);
		}else if (age < 16){
			throw new MyException(ResultEnum.MIDDLE_SCHOLL);
		}
		
		// 其他非异常处理
		// ......
	}
}
